package com.igeek.web;

import com.google.gson.Gson;
import com.igeek.domain.Category;
import com.igeek.service.Categoryservice;
import com.igeek.utils.JedisPoolUtils;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/category")
public class CategoryServlet extends BaseServlet {
    public void findcategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Jedis jedis=null;
        String gsonstring="";
        try {
            jedis=JedisPoolUtils.getJedis();
        } catch (Exception e) {
            Categoryservice service =new Categoryservice();
            List<Category> list = service.findallCategory();
            Gson gson=new Gson();
            gsonstring=gson.toJson(list);
            response.getWriter().write(gsonstring);
            return;
        }
        gsonstring=jedis.get("category");
        if (gsonstring==null){
            Categoryservice service =new Categoryservice();
            List<Category> list = service.findallCategory();
            Gson gson=new Gson();
            gsonstring=gson.toJson(list);
        }
        response.getWriter().write(gsonstring);
    }
}
